<?php
namespace app\admin\controller;
use think\Db;
use lib\Curl;
class Member extends Base{
    public function member_list(){
        $key=input('get.key');
        if (!empty(input('get.fromtime'))) {
            $fromtime=strtotime(input('get.fromtime'));
        }else{
            $fromtime=0;
        }
        if (!empty(input('get.totime'))) {
            $totime=strtotime(input('get.totime'))+86400;
        }else{
            $totime=time();
        }
	        $list=Db::name('users')->where('time','>',"{$fromtime}")->where('time','<',"{$totime}")->where('name|phone|idcard','like',"%{$key}%")->order('id desc')->paginate(10);
	        $lists=Db::name('users')->where('time','>',"{$fromtime}")->where('time','<',"{$totime}")->where('name|phone|idcard','like',"%{$key}%")->select();
        
        $this->assign('key',input('get.key'));
        $this->assign('fromtime',input('get.fromtime'));
        $this->assign('totime',input('get.totime'));
        $this->assign('list',$list);
        $this->assign('lists',$lists);
        return $this->fetch();
    }
    public function member_detail(){
        if(request()->isGet()){
            $lengthone=0;
            $lengthtwo=0;
            $lengththree=0;
            $id = input('param.id');
            $res = Db::name('contacts')->where('user_id',$id)->order('id desc')->limit(1)->find();
            if ($res) {
                $contacts=json_decode(json_decode($res['list'],true),true);
                $lengthone=ceil(count($contacts)/3);
                $lengthtwo=2*ceil(count($contacts)/3);
                $lengththree=count($contacts);
            }else{
                $contacts=null;
            }
            $userinfo = Db::name('users')->where('id',$id)->find();
            if ($userinfo['bank_cdid']) {
                $bank= Db::name('bank')->where('id',$userinfo['bank_cdid'])->find();
                $userinfo['bank_bank']=$bank['bank'];
            }else{
                $userinfo['bank_bank']=null;
            }
            $userinfo['exit_relatives']=0;
            $userinfo['exit_emer_con']=0;
            $userinfo['exit_partner']=0;
            $userinfo['exit_friend']=0;
            $orders = Db::name('orders')->alias('a')->join('product b', 'a.pro_id = b.id','LEFT')->join('customer c', 'a.cus_id = c.id','LEFT')->field('a.*,b.title,b.moneyf,b.moneyt,b.cycle,c.cname')->where('a.user_id',$id)->order('a.id desc')->select();
            if (empty($orders)) {
                $orders=null;
            }
            $result = Db::name('yysreport')->where('user_id',$id)->find();
            if ($result) {
                $yysreport=json_decode($result['list']);
                if($yysreport!=null && $yysreport->resCode==0){
                    $resData=$yysreport->resData;
                    if($yysreport->code==0 && $yysreport->resCode==0){
                        $userBasic=$resData->userBasic;
                        $cellPhone=$resData->cellPhone;
                        $tripInfo=$resData->tripInfo;
                        $friendCircle=$resData->friendCircle;
                        $peerNumTop33m=$friendCircle->peerNumTop33m;
                        $peerNumTop36m=$friendCircle->peerNumTop36m;
                        $localTop33m=$friendCircle->localTop33m;
                        $localTop36m=$friendCircle->localTop36m;
                        $cellBehavior=$resData->cellBehavior;
                        $behavior=$cellBehavior[0]->behavior;
                        $callContactDetail=$resData->callContactDetail;
                        // var_dump($callContactDetail[0]->city);die;
                        $callRiskAnalysis=$resData->callRiskAnalysis;
                        $activeDegree=$resData->activeDegree;
                        $consumptionDetail=$resData->consumptionDetail;
                        $callTimeDetail=$resData->callTimeDetail;
                        $callDurationDetail=$resData->callDurationDetail;
                        $duration3=$callDurationDetail[0]->duration;
                        $duration6=$callDurationDetail[1]->duration;
                        $date1=date('Y-m-d',strtotime($cellPhone->regTime));
                        $date2=date('Y-m-d',time());
                        $ymd=$this->diffDate($date1,$date2);
                        for ($i=0; $i <count($callContactDetail) ; $i++) { 
                            if(str_replace(' ','',$userinfo['relatives_phone'])==$callContactDetail[$i]->peerNumber){
                                $userinfo['exit_relatives']=1;
                            }
                            if(str_replace(' ','',$userinfo['emer_con_phone'])==$callContactDetail[$i]->peerNumber){
                                $userinfo['exit_emer_con']=1;
                            }
                            if(str_replace(' ','',$userinfo['partner_phone'])==$callContactDetail[$i]->peerNumber){
                                $userinfo['exit_partner']=1;
                            }
                            if(str_replace(' ','',$userinfo['friend_phone'])==$callContactDetail[$i]->peerNumber){
                                $userinfo['exit_friend']=1;
                            }
                        }
                        $this->assign('ymd',$ymd);
                        $this->assign('yysreport',$yysreport);
                        $this->assign('resData',$resData);
                        $this->assign('userBasic',$userBasic);
                        $this->assign('cellPhone',$cellPhone);
                        $this->assign('tripInfo',$tripInfo);
                        $this->assign('friendCircle',$friendCircle);
                        $this->assign('peerNumTop33m',$peerNumTop33m);
                        $this->assign('peerNumTop36m',$peerNumTop36m);
                        $this->assign('localTop33m',$localTop33m);
                        $this->assign('localTop36m',$localTop36m);
                        $this->assign('cellBehavior',$cellBehavior);
                        $this->assign('behavior',$behavior);
                        $this->assign('callContactDetail',$callContactDetail);
                        $this->assign('callRiskAnalysis',$callRiskAnalysis);
                        $this->assign('activeDegree',$activeDegree);
                        $this->assign('consumptionDetail',$consumptionDetail);
                        $this->assign('callTimeDetail',$callTimeDetail);
                        $this->assign('callDurationDetail',$callDurationDetail);
                        $this->assign('duration3',$duration3);
                        $this->assign('duration6',$duration6);
                        $this->assign('yysreports',1);
                    }else{
                            $this->assign('yysreports',0);
                    }
                }else{
                    $yysSate = Db::name('users')->where('id',$id)->value('yys_state');
                    if ($yysSate>0) {
                        $yysreports = Curl::http_curl("http://www.kbb123.com/api/meta/yysreport?user_id={$id}");
                        Db::name('yysreport')->where('user_id', $id)->update(['list'=>$yysreports,'addtime'=>time()]);
                        $yysreport=json_decode($yysreports);
                        $resData=$yysreport->resData;
                        if($yysreport->code==0 && $yysreport->resCode==0){
                            $userBasic=$resData->userBasic;
                            $cellPhone=$resData->cellPhone;
                            $tripInfo=$resData->tripInfo;
                            $friendCircle=$resData->friendCircle;
                            $peerNumTop33m=$friendCircle->peerNumTop33m;
                            $peerNumTop36m=$friendCircle->peerNumTop36m;
                            $localTop33m=$friendCircle->localTop33m;
                            $localTop36m=$friendCircle->localTop36m;
                            $cellBehavior=$resData->cellBehavior;
                            $behavior=$cellBehavior[0]->behavior;
                            $callContactDetail=$resData->callContactDetail;
                            $callRiskAnalysis=$resData->callRiskAnalysis;
                            $activeDegree=$resData->activeDegree;
                            $consumptionDetail=$resData->consumptionDetail;
                            $callTimeDetail=$resData->callTimeDetail;
                            $callDurationDetail=$resData->callDurationDetail;
                            $duration3=$callDurationDetail[0]->duration;
                            $duration6=$callDurationDetail[1]->duration;
                        $date1=date('Y-m-d',strtotime($cellPhone->regTime));
                        $date2=date('Y-m-d',time());
                        $ymd=$this->diffDate($date1,$date2);
                        for ($i=0; $i <count($callContactDetail) ; $i++) { 
                            if(str_replace(' ','',$userinfo['relatives_phone'])==$callContactDetail[$i]->peerNumber){
                                $userinfo['exit_relatives']=1;
                            }
                            if(str_replace(' ','',$userinfo['emer_con_phone'])==$callContactDetail[$i]->peerNumber){
                                $userinfo['exit_emer_con']=1;
                            }
                            if(str_replace(' ','',$userinfo['partner_phone'])==$callContactDetail[$i]->peerNumber){
                                $userinfo['exit_partner']=1;
                            }
                            if(str_replace(' ','',$userinfo['friend_phone'])==$callContactDetail[$i]->peerNumber){
                                $userinfo['exit_friend']=1;
                            }
                        }
                        $this->assign('ymd',$ymd);
                            $this->assign('yysreport',$yysreport);
                            $this->assign('resData',$resData);
                            $this->assign('userBasic',$userBasic);
                            $this->assign('cellPhone',$cellPhone);
                            $this->assign('tripInfo',$tripInfo);
                            $this->assign('friendCircle',$friendCircle);
                            $this->assign('peerNumTop33m',$peerNumTop33m);
                            $this->assign('peerNumTop36m',$peerNumTop36m);
                            $this->assign('localTop33m',$localTop33m);
                            $this->assign('localTop36m',$localTop36m);
                            $this->assign('cellBehavior',$cellBehavior);
                            $this->assign('behavior',$behavior);
                            $this->assign('callContactDetail',$callContactDetail);
                            $this->assign('callRiskAnalysis',$callRiskAnalysis);
                            $this->assign('activeDegree',$activeDegree);
                            $this->assign('consumptionDetail',$consumptionDetail);
                            $this->assign('callTimeDetail',$callTimeDetail);
                            $this->assign('callDurationDetail',$callDurationDetail);
                            $this->assign('duration3',$duration3);
                            $this->assign('duration6',$duration6);
                            $this->assign('yysreports',1);
                        }else{
                            $this->assign('yysreports',0);
                        }
                    }else{
                        $this->assign('yysreports',0);
                    }
                }
            }else{
                $yysSate = Db::name('users')->where('id',$id)->value('yys_state');
                if ($yysSate==1) {
                    $yysreports = Curl::http_curl("http://www.kbb123.com/api/meta/yysreport?user_id={$id}");
                    $insert=[
                        'user_id'   => $id,
                        'list'      => $yysreports,
                        'addtime'       => time(),
                    ];
                    Db::name('yysreport')->insert($insert);
                    $yysreport=json_decode($yysreports);
                    $resData=$yysreport->resData;
                        if($yysreport->code==0 && $yysreport->resCode==0){
                            $userBasic=$resData->userBasic;
                            $cellPhone=$resData->cellPhone;
                            $tripInfo=$resData->tripInfo;
                            $friendCircle=$resData->friendCircle;
                            $peerNumTop33m=$friendCircle->peerNumTop33m;
                            $peerNumTop36m=$friendCircle->peerNumTop36m;
                            $localTop33m=$friendCircle->localTop33m;
                            $localTop36m=$friendCircle->localTop36m;
                            $cellBehavior=$resData->cellBehavior;
                            $behavior=$cellBehavior[0]->behavior;
                            $callContactDetail=$resData->callContactDetail;
                            $callRiskAnalysis=$resData->callRiskAnalysis;
                            $activeDegree=$resData->activeDegree;
                            $consumptionDetail=$resData->consumptionDetail;
                            $callTimeDetail=$resData->callTimeDetail;
                            $callDurationDetail=$resData->callDurationDetail;
                            $duration3=$callDurationDetail[0]->duration;
                            $duration6=$callDurationDetail[1]->duration;
                        $date1=date('Y-m-d',strtotime($cellPhone->regTime));
                        $date2=date('Y-m-d',time());
                        $ymd=$this->diffDate($date1,$date2);
                        for ($i=0; $i <count($callContactDetail) ; $i++) { 
                            if(str_replace(' ','',$userinfo['relatives_phone'])==$callContactDetail[$i]->peerNumber){
                                $userinfo['exit_relatives']=1;
                            }
                            if(str_replace(' ','',$userinfo['emer_con_phone'])==$callContactDetail[$i]->peerNumber){
                                $userinfo['exit_emer_con']=1;
                            }
                            if(str_replace(' ','',$userinfo['partner_phone'])==$callContactDetail[$i]->peerNumber){
                                $userinfo['exit_partner']=1;
                            }
                            if(str_replace(' ','',$userinfo['friend_phone'])==$callContactDetail[$i]->peerNumber){
                                $userinfo['exit_friend']=1;
                            }
                        }
                        $this->assign('ymd',$ymd);
                            $this->assign('yysreport',$yysreport);
                            $this->assign('resData',$resData);
                            $this->assign('userBasic',$userBasic);
                            $this->assign('cellPhone',$cellPhone);
                            $this->assign('tripInfo',$tripInfo);
                            $this->assign('friendCircle',$friendCircle);
                            $this->assign('peerNumTop33m',$peerNumTop33m);
                            $this->assign('peerNumTop36m',$peerNumTop36m);
                            $this->assign('localTop33m',$localTop33m);
                            $this->assign('localTop36m',$localTop36m);
                            $this->assign('cellBehavior',$cellBehavior);
                            $this->assign('behavior',$behavior);
                            $this->assign('callContactDetail',$callContactDetail);
                            $this->assign('callRiskAnalysis',$callRiskAnalysis);
                            $this->assign('activeDegree',$activeDegree);
                            $this->assign('consumptionDetail',$consumptionDetail);
                            $this->assign('callTimeDetail',$callTimeDetail);
                            $this->assign('callDurationDetail',$callDurationDetail);
                            $this->assign('duration3',$duration3);
                            $this->assign('duration6',$duration6);
                            $this->assign('yysreports',1);
                        }else{
                            $this->assign('yysreports',0);
                        }
                }else{
                    $this->assign('yysreports',0);
                }
            }
            $alldays = Db::name('allday')->where('user_id',$id)->find();
            if ($alldays) {
                $allday = json_decode($alldays['list'],true);
                $registerPlatforms=$allday['resData']['multiRegisterRes']['registerPlatforms'];
                $overduePlatforms=$allday['resData']['overdueRes']['overduePlatforms'];
                $loanPlatforms=$allday['resData']['creditableRes']['loanPlatforms'];
                $repaymentPlatforms=$allday['resData']['favourableRes']['repaymentPlatforms'];
                $refusePlatforms=$allday['resData']['rejecteeRes']['refusePlatforms'];
                $this->assign('allday',$allday);
                $this->assign('registerPlatforms',$registerPlatforms);
                $this->assign('overduePlatforms',$overduePlatforms);
                $this->assign('loanPlatforms',$loanPlatforms);
                $this->assign('repaymentPlatforms',$repaymentPlatforms);
                $this->assign('refusePlatforms',$refusePlatforms);
            }else{
                $alldays = Curl::http_curl("http://www.kbb123.com/api/meta/allday?user_id={$id}");
                $insertall=[
                    'user_id'   => $id,
                    'list'      => $alldays,
                    'addtime'       => time(),
                ];
                Db::name('allday')->insert($insertall);
                $allday = json_decode($alldays,true);
                $registerPlatforms=$allday['resData']['multiRegisterRes']['registerPlatforms'];
                $overduePlatforms=$allday['resData']['overdueRes']['overduePlatforms'];
                $loanPlatforms=$allday['resData']['creditableRes']['loanPlatforms'];
                $repaymentPlatforms=$allday['resData']['favourableRes']['repaymentPlatforms'];
                $refusePlatforms=$allday['resData']['rejecteeRes']['refusePlatforms'];
                $this->assign('allday',$allday);
                $this->assign('registerPlatforms',$registerPlatforms);
                $this->assign('overduePlatforms',$overduePlatforms);
                $this->assign('loanPlatforms',$loanPlatforms);
                $this->assign('repaymentPlatforms',$repaymentPlatforms);
                $this->assign('refusePlatforms',$refusePlatforms);
            }
            $this->assign('lengthone',$lengthone);
            $this->assign('lengthtwo',$lengthtwo);
            $this->assign('lengththree',$lengththree);
            $this->assign('id',$id);
            $this->assign('contacts',$contacts);
            $this->assign('userinfo',$userinfo);
            $this->assign('orders',$orders);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            // $data['username']=$param['username'];
            $data['uptime']=time();
            $data['state']=2;
            // $res=Db::name('admin')->insert($data);
            return json(['code' => 200, 'data' =>$data, 'msg' => '调试']);
        }
    }
    public function contacts_page(){
        $id = input('user_id');
        $res = Db::name('contacts')->where('user_id',$id)->order('id desc')->limit(1)->find();
        $data=json_decode(json_decode($res['list']));
        return json(['code' => 200, 'data' =>$data, 'msg' => '成功']);
    }
    public function calldetail_page(){
        $id = input('user_id');
        $res = Db::name('yysreport')->where('user_id',$id)->find();
        $data=json_decode($res['list']);
        return json(['code' => 200, 'data' =>$data, 'msg' => '成功']);
    }
    public function oten(){
        $id = input('user_id');
        $user = Db::name('users')->where('id',$id)->find();
        $res = Db::name('yysreport')->where('user_id',$id)->find();
        $data=json_decode($res['list']);
        $resc = Db::name('contacts')->where('user_id',$id)->order('id desc')->limit(1)->find();
        $datac=json_decode(json_decode($resc['list']));
        $result=$this->resosort($data->resData->callContactDetail);
        for ($i=0; $i <count($datac); $i++) { 
            for ($j=0; $j <count($result); $j++) {
                if(!empty($datac[$i]->phoneNumbers[0])){
                    str_replace('-','',$datac[$i]->phoneNumbers[0]->value);
                    str_replace(' ','',$datac[$i]->phoneNumbers[0]->value);
                    if(substr($result[$j]->peerNumber,-11)==substr($datac[$i]->phoneNumbers[0]->value,-11)){
                        $result[$j]->txlxm=$datac[$i]->displayName;
                        if(substr($result[$j]->peerNumber,-11)==substr($datac[$i]->phoneNumbers[0]->value,-11)&&substr($result[$j]->peerNumber,-11)==substr($user['relatives_phone'],-11)){
                            $result[$j]->peerNumber='<span style="background-color:#5eb95e;">'.$result[$j]->peerNumber.'</span>';
                        }elseif(substr($result[$j]->peerNumber,-11)==substr($datac[$i]->phoneNumbers[0]->value,-11)&&substr($result[$j]->peerNumber,-11)==substr($user['emer_con_phone'],-11)){
                            $result[$j]->peerNumber='<span style="background-color:#ffeab9;">'.$result[$j]->peerNumber.'</span>';
                        }elseif(substr($result[$j]->peerNumber,-11)==substr($datac[$i]->phoneNumbers[0]->value,-11)&&substr($result[$j]->peerNumber,-11)==substr($user['partner_phone'],-11)){
                             $result[$j]->peerNumber='<span style="background-color:#6db3e3;">'.$result[$j]->peerNumber.'</span>';
                        }elseif(substr($result[$j]->peerNumber,-11)==substr($datac[$i]->phoneNumbers[0]->value,-11)&&substr($result[$j]->peerNumber,-11)==substr($user['friend_phone'],-11)){
                            $result[$j]->peerNumber='<span style="background-color:#009393;">'.$result[$j]->peerNumber.'</span>';
                        }
                    }
                }
            }
        }
        return json(['code' => 200, 'data' =>$result, 'msg' => '成功']);
    }
    public function eten(){
        $id = input('user_id');
        $user = Db::name('users')->where('id',$id)->find();
        $res = Db::name('yysreport')->where('user_id',$id)->find();
        $data=json_decode($res['list']);
        $resc = Db::name('contacts')->where('user_id',$id)->order('id desc')->limit(1)->find();
        $datac=json_decode(json_decode($resc['list']));
        $result=$this->resosort($data->resData->callContactDetail);
        for ($i=0; $i <count($datac); $i++) { 
            for ($j=0; $j <count($result); $j++) {
                if(!empty($datac[$i]->phoneNumbers[0])){
                    str_replace('-','',$datac[$i]->phoneNumbers[0]->value);
                    str_replace(' ','',$datac[$i]->phoneNumbers[0]->value);
                    if(substr($result[$j]->peerNumber,-11)==substr($datac[$i]->phoneNumbers[0]->value,-11)){
                        $result[$j]->txlxm=$datac[$i]->displayName;
                        if(substr($result[$j]->peerNumber,-11)==substr($datac[$i]->phoneNumbers[0]->value,-11)&&substr($result[$j]->peerNumber,-11)==substr($user['relatives_phone'],-11)){
                            $result[$j]->peerNumber='<span style="background-color:#5eb95e;">'.$result[$j]->peerNumber.'</span>';
                        }elseif(substr($result[$j]->peerNumber,-11)==substr($datac[$i]->phoneNumbers[0]->value,-11)&&substr($result[$j]->peerNumber,-11)==substr($user['emer_con_phone'],-11)){
                            $result[$j]->peerNumber='<span style="background-color:#ffeab9;">'.$result[$j]->peerNumber.'</span>';
                        }elseif(substr($result[$j]->peerNumber,-11)==substr($datac[$i]->phoneNumbers[0]->value,-11)&&substr($result[$j]->peerNumber,-11)==substr($user['partner_phone'],-11)){
                             $result[$j]->peerNumber='<span style="background-color:#6db3e3;">'.$result[$j]->peerNumber.'</span>';
                        }elseif(substr($result[$j]->peerNumber,-11)==substr($datac[$i]->phoneNumbers[0]->value,-11)&&substr($result[$j]->peerNumber,-11)==substr($user['friend_phone'],-11)){
                            $result[$j]->peerNumber='<span style="background-color:#009393;">'.$result[$j]->peerNumber.'</span>';
                        }
                    }
                }
            }
        }
        return json(['code' => 200, 'data' =>$result, 'msg' => '成功']);
    }
    private function resesort($data){
        for($i=1;$i<count($data);$i++){ //该层循环用来控制每轮 冒出一个数 需要比较的次数
            for($k=0;$k<count($data)-$i;$k++){
                if($data[$k]->dialedCnt6m<$data[$k+1]->dialedCnt6m){
                    $tmp=$data[$k+1];
                    $data[$k+1]=$data[$k];
                    $data[$k]=$tmp;
                }
            }
        }
        return $data;
    }
    private function resosort($data){
        for($i=1;$i<count($data);$i++){ //该层循环用来控制每轮 冒出一个数 需要比较的次数
            for($k=0;$k<count($data)-$i;$k++){
                if($data[$k]->dialCnt6m<$data[$k+1]->dialCnt6m){
                    $tmp=$data[$k+1];
                    $data[$k+1]=$data[$k];
                    $data[$k]=$tmp;
                }
            }
        }
        return $data;
    }
    private function diffDate($date1,$date2){ 
        if(strtotime($date1)>strtotime($date2)){ 
            $tmp=$date2; 
            $date2=$date1; 
            $date1=$tmp; 
        } 
        list($Y1,$m1,$d1)=explode('-',$date1); 
        list($Y2,$m2,$d2)=explode('-',$date2); 
        $Y=$Y2-$Y1; 
        $m=$m2-$m1; 
        $d=$d2-$d1; 
        if($d<0){ 
            $d+=(int)date('t',strtotime("-1 month $date2")); 
            $m--; 
        } 
        if($m<0){ 
            $m+=12; 
            $Y--; 
        } 
        return array('year'=>$Y,'month'=>$m,'day'=>$d); 
    } 
}
